<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('数据源列表')" />
</head>
<body class="pear-container">
<!-- 查询栏 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="col-sm-12 search-collapse">
                <form id="formId" class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">项目名称：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="请输入连接名" class="layui-input" />
                        </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="data-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 数据表头操作按钮 -->
    <script type="text/html" id="data-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
    </script>

    <!-- 数据列表 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="data-table" lay-filter="data-table"></table>
        </div>
    </div>

    <!-- 列表操作按钮 -->
    <script type="text/html" id="operate-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="linkTest">
            <i class="layui-icon layui-icon-link"></i>
        </button>
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </script>

</body>
<th:block th:include="include :: footer" />

<script type="text/html" id="type_parseDictText">
    {{layui.dictionary.parseDictTexts(d.type,  'DB_TYPE')}}
</script>

<script>
    layui.use(['table', 'form', 'jquery','dictionary'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = "/meta/dataSource/";

        let cols = [
            [
                {
                    field: 'id',
                    title: '主键ID'
                },
                {
                    field: 'name',
                    title: '项目名称'
                },
                {
                    field: 'host',
                    title: '主机'
                },
                {
                    field: 'port',
                    title: '端口'
                },
                {
                    field: 'serviceName',
                    title: '数据库名称'
                },
                {
                    field: 'type',
                    title: '数据库类型',
                    templet: "#type_parseDictText"
                },
                {
                    field: 'username',
                    title: '用户名'
                },
                {
                    title: '操作',
                    toolbar: '#operate-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        table.render({
            elem: '#data-table',
            url: prefix + 'pageList',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#data-toolbar',
            defaultToolbar: [
                'filter', 'exports'
            ]
        });

        table.on('tool(data-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'linkTest') {
                window.linkTest(obj);
            }
        });

        table.on('toolbar(data-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(data-query)', function (data) {
            table.reload('data-table', {where: data.field})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view?id=' + obj.data['id']
            });
        }

        window.linkTest = function (obj) {
            $.ajax({
                    url: prefix + "linkTest/" + obj.data['id'],
                    dataType: 'json',
                    type: 'get',
                    success: function (result) {
                        if (result.success) {
                            layer.msg('连接成功', {icon: 1, time: 1000});
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该${functionName}', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('data-table', {where: param});
        }
    })
</script>
</html>